%**************************************************************
% Reads the data from ultrasound (.bin) and wire-mesh (.v) files
% Autor: Carlos Amaral (ceamaral@hotmail.com)
% data: 06/10/2010
% functions that are called: read_US, read_WM
% sensor_type can be 'US' or 'WM'
% num_channels can be '1_channels' or '2_channels'
%**************************************************************

function read_some_measurements(files_list, sensor_type,num_channels,freq_aq,tube_diameter)
% If sensor_type is 'US', analysis of ultrasound
% If sensor_type is 'WM', analysis of wire-mesh
% If num_channels is '1_channels' process only sensor 1
% If num_channels is '2_channels' process only sensor 2

close all %Closes all Figures already open




num_files = length(files_list);
if(num_files > 0)          %testa se existem arquivos .
    fprintf('\n Reading %s sensor (%s).\n',sensor_type,num_channels);
    nr=0; 
    for i=1:num_files                                  %Creates a matrix of JL and JG using the name of the files_list                             
        if (strcmp(sensor_type,'US'))  %looks for files types  
            fprintf('     Reading %s data\n', files_list(i).name);
            name_file = files_list(i).name;
            openfile=[files_list(i).name '.bin'];
           [void_US1,waterlevel_US1] = read_US(openfile,tube_diameter,freq_aq,'US1');   %Calculates alpha from .BIN ultrasound files
           if (strcmp(num_channels,'2_channels'))   %looks for files types   
                [void_US2,waterlevel_US2] = read_US(openfile,tube_diameter,freq_aq,'US2');   %Calculates alpha from .BIN ultrasound files
           end
        end
        
        if (strcmp(sensor_type,'WM')) %looks for files types   
            
            fprintf('     Reading %s data\n', files_list(i).name);
            name_file = files_list(i).name;
            openfile=[files_list(i).name '-8_1.v'];
            [void_WM1,midle_electrode_void_WM1,waterlevel_WM1] = read_WM(openfile,tube_diameter);  %returns the mean void fraction and the midle electrode measurements            
            if (strcmp(num_channels,'2_channels')) && (num_files_WM2 > 0)   %looks for files types                   
                openfile=[files_list(i).name '-8_2.v'];
                fprintf('     Reading %s data\n', openfile);                
                [void_WM2,midle_electrode_void_WM2,waterlevel_WM2] = read_WM(openfile,tube_diameter);                                   
            end        
        end            
    
        if (exist('.\Matlab_data','dir') == 0) %Creates Result directory if it does not exist yeat
          mkdir('Matlab_data');  
        end          
        
        file_path_output = ['.\Matlab_data\' name_file '_' sensor_type '.mat']; %Saves workspace in \Matlab_data directory
        if (exist(file_path_output,'file') == 0) %Erases data before
          delete(file_path_output);  
        end
        save(file_path_output);          
    end
   
end

return